<?php

namespace app\index\controller;

use app\common\model\AppraiseModel;
use think\Controller;
use think\Session;
use think\Db;
use think\Request;
use think\Cache;

class Login
{

    public function login()
    {

        if (request()->isAjax()) {

            $param = request()->param();

            $param = $param['data'];

            //检查账号密码
            //加密方式
            $hasher = new \PasswordHash(8, false);
            $check = Db::name('user')->where("phone", $param['phone'])->find();

            if (!empty($check)) {
                if ($hasher->CheckPassword($param['pwd'], $check['password'])) {

                    Session::set('uid', $check['uid']);
                    Session::set('nickname', $check['nickname']);
                    Session::set('user_icon', $check['user_icon']);

                    //TODO::用户最新登录时间是否需要
                    return ['code' => 0];

                } else {
                    return ['code' => '密码错误'];
                }

            } else {
                return ['code' => '账号不存在'];
            }

        } else {

            return view();

        }

    }

    public function getIcode(){
        if (Request::instance()->isAjax()){
            $post  = Request::instance()->post();

            $res = sendJsmsCode($post['regphone'],null);

            if($res['http_code'] == '200'){
                $cache_name = "jsms_".$post['regphone'];
                Cache::set($cache_name,$res['body']['msg_id'],1000);
                return ['code'=>0];
            }else{
                return ['code' => '发送失败'];
            }

        }
    }

    public function register(){

        if (Request::instance()->isAjax()){

            $param = request()->post();

            $param = $param['data'];

            //检查验证码是否错误
            $cache_name = "jsms_".$param['phone'];
            $msg_id = Cache::get($cache_name);
            if (!$msg_id){
                return ['code'=>1,'msg'=>'短信验证码错误'];
            }else{
                $res = checkJsmsCode($msg_id,$param['icode']);
                if ($res['body']['is_valid']){

                    //检查手机号是否存在
                    $check  = Db::name('user')->where("phone",$param['phone'])->find();

                    if (!empty($check)){

                        return ['code'=>1,'msg'=>'手机号已存在'];

                    }else{

                        //加密方式
                        $hasher = new \PasswordHash(8, false);
                        //将密码进行哈希，结果是一个长度为60个字符的字符串
                        $param['password'] = $hasher->HashPassword($param['password']);

                        $addData = [
                            'phone' => $param['phone'],
                            'password' =>$param['password'],
                            'nickname' =>$param['nickname'],
                            'create_time'=>date("Y-m-d H:i:s"),
                            'user_icon'=>'http://qiniu.lancly.com//index/user/dafaultUserIcon.png'
                        ];
                        $res = Db::name('user')->insertGetId($addData);
                        if ($res){
                            Session::set('uid', $res);
                            Session::set('nickname', $addData['nickname']);
                            Session::set('user_icon', 'http://qiniu.lancly.com//index/user/dafaultUserIcon.png');
                            return ['code'=>'0'];
                        }else{
                            return ['code'=>1,'msg'=>'添加失败'];
                        }
                    }

                }elseif($res['body']['error']['code'] == '50010'){

                    return ['code'=>1,'msg'=>'短信验证码错误'];

                }elseif($res['body']['error']['code'] == '50011'){

                    return ['code'=>1,'msg'=>'短信验证码过期'];

                }elseif($res['body']['error']['code'] == '50012'){

                    return ['code'=>1,'msg'=>'短信验证码错误'];
                }
            }

        }
        
    }

    //极光验证码验证
    protected function checkJsmsCode($msg_id,$code){

        import("Jsms.JSMS");

        $app_key = config('jpush.app_key');

        $master_secret = config('jpush.master_secret');

        $client = new \JSMS($app_key, $master_secret);

        $response = $client->checkCode($msg_id, $code);

        return $response;
    }

    public function addWantApply(){

        if (request()->isAjax()) {
            $param = request()->param();
            $param = $param['data'];
            $addData = [

                'user_name' => $param['user_name'],
                'user_tel' => $param['user_tel'],
                'store_name' => $param['store_name'],
                'create_time'=> date('Y-m-d H:i:s'),
                'type' => $param['type']

            ];

            $res = Db::name('want_apply')->insertGetId($addData);

            if ($res) {
                return ['code'=>1];
            }else{
                return ['code'=>0];
            }
        }

    }
}